import pickerTime from './components/pickerTime.vue'
// import { AddMobileContactData } from '@/utils/common-api.js'
export default {
  data() {
    return {
      more: false,
      Pregoods: false,
      timeStatus: false, //选择预约时间
      timeAll: {}, //所选预约时间
      OrderMakeTime: '', //预约时间
      showTime: '', //页面显示时间
      lineUpList: [],
      lineUpcurrent: 0, //排队tabcurrent
      orderid: '',
      lon: 0,
      lat: 0,
      status: [{
        val: 0,
        name: '待确认'
      },
      {
        val: 5,
        name: '待付款'
      },
      {
        val: 10,
        name: '已取消'
      },
      {
        val: 20,
        name: '待备货'
      },
      {
        val: 30,
        name: '待服务'
      },
      {
        val: 40,
        name: '服务中-排队中'
      },
      {
        val: 50,
        name: '服务中'
      },
      {
        val: 60,
        name: '已完成'
      }
      ],
      checkArrText: ['预检', '上检', '质检/交车'],
      dataObj: {
        servicelist: [],
        pay_time: '',
        queuedlist: [],
        examinelist: [],
        is_car_wash: false
      },
      mycarindex: null, //传参时我的爱车下标
      write: false,
      userWrite: '', //用户确认签字
      checkInd: 0, //检查结果选的检查项
      oneshow: false,
			OrderMakeTimeStatus:'',//预约状态
    }
  },
  
  computed: {
    serviceNumber(){
      return this.$store.state.serviceNumber
    }
  },
  components: {
    pickerTime
  },
  onLoad(options) {
    let that = this
    this.orderid = options.id
    uni.getLocation({
      type: 'wgs84',
      success: function(res) {
        that.lon = res.longitude
        that.lat = res.latitude
        console.log('当前位置的经度：' + res.longitude)
        console.log('当前位置的纬度：' + res.latitude)
        that.GetClientOrderDetails()
      },
      fail() {
        that.lon = 0
        that.lat = 0
        that.GetClientOrderDetails()
      }
    })

    uni.$on('userConfirm', function(data) {
      that.$nextTick(function() {
        that.userWrite = data.userName
      })
    })

  },
  onShow() {
    console.log(11, this.oneshow)
    if (this.oneshow) {
      this.GetClientOrderDetails()
    }
  },
  methods: {
    // 获取详情信息
    GetClientOrderDetails() {
      let data = {
        'orderid': this.orderid,
        'lon': this.lon,
        'lat': this.lat
      }
      // console.log(data)
      this.$api.maintenanceOrder.GetClientOrderDetails(data).then(res => {
        console.log(res.data)
        this.dataObj = res.data
        this.oneshow = true
        if (res.data.appointment_time) {
          var myDate = new Date(res.data.appointment_time)
          var str = ' 星期' + '日一二三四五六 '.charAt(myDate.getDay())
          this.showTime = res.data.appointment_time.split('T')[0] + str
					this.OrderMakeTimeStatus=true
					this.OrderMakeTime=res.data.appointment_time
        }

        if (res.data.status === 40) {
          res.data.queuedlist.forEach(it => {
            this.lineUpList.push({
              name: it.name
            })
          })
        } else if (res.data.status === 5) {
          let data = Math.round(new Date() / 1000)
          console.log(data-Number(res.data.pay_deadline)
          )
          if(data-Number(res.data.pay_deadline)>0){
            res.data.pay_deadline =7200-((Math.abs(data-Number(res.data.pay_deadline)))%7200)
          }else{
            res.data.pay_deadline=	Math.abs(data-Number(res.data.pay_deadline))
          }
          // res.data.pay_deadline =7200-((Math.abs(data-Number(res.data.pay_deadline)))%7200)
        } else if (res.data.status === 0) {
          let data = Math.round(new Date() / 1000)
          console.log(data-Number(res.data.affirm_deadline)
          )
          // if(data-Number(res.data.affirm_deadline)>0){
          // 	res.data.affirm_deadline =7200-((Math.abs(data-Number(res.data.affirm_deadline)))%7200)
          // }else{
          // res.data.affirm_deadline=	Math.abs(data-Number(res.data.affirm_deadline))
          // }
          res.data.affirm_deadline =7200-((Math.abs(data-Number(res.data.affirm_deadline)))%7200)

        }
        console.log(res.data)
        if (res.data.status === 60) {
          this.checkInd = 2
        }
        this.GetCarList()
      }).catch(err => {
        console.log(err)
      })
    },
    // 去付款
    gopay() {

      uni.navigateTo({
        url: '/pages_commodity/payment/payment?type=maintenance&orderNo=' + this.orderid + '&price=' +
					this.dataObj
					  .amount_payable
      })
    },
    // 返回状态
    returnStatus(val) {
      let statusTxt = ''
      this.status.forEach(it => {
        if (it.val === val) {
          statusTxt = it.name
        }
      })
      return statusTxt
    },
    // 返回时间
    returnTime(val) {
      console.log(val)
      if (val) {
        return val.split('T')[0] + ' ' + val.split('T')[1]
      }
    },
    // 获取预约时间
    getTimeVal(val) {
      console.log(val)
      this.timeAll = val
      let m = '',
        d = ''
      if (val.month < 10) {
        m = '0' + val.month
      } else {
        m = val.month
      }
      if (val.day < 10) {
        d = '0' + val.day
      } else {
        d = val.day
      }
      this.OrderMakeTime = val.year + '-' + m + '-' + d + ' ' + val.hour + ':' + val.minute + ':00'
      this.showTime = val.year + '年' + val.month + '月' + val.day + '日 ' + val.week + ' ' + val.hour + ':' + val
        .minute
      this.timeStatus = false
    },
    goWrite() {

      uni.navigateTo({
        url: '/pages_common/signature/signature'
      })

    },
    // 点击导航
    onClickNavigation() {
      // console.log(222)
      uni.openLocation({
        latitude: this.dataObj.lat,
        longitude: this.dataObj.lon,
        name: this.dataObj.store_name,
        address: this.dataObj.store_address
      })
    },
    // 签名确认
    QuatationSure() {
      let data = {
        'orderId': this.orderid,
        'signImg': this.userWrite,
        'mobile': '',
        'identityCode': '',
        'type': 0
      }
      if (this.userWrite) {
        this.$api.common.QuatationSure(data).then(res => {
          console.log(res)
          this.write = true
          uni.showToast({
            title: '已确认',
            duration: 2000
          })
        }).catch(err => {
          console.log(err)
        })
      } else {
        uni.showToast({
          title: '请先签字确认',
          icon: 'none',
          duration: 2000
        })
      }
    },
    // 取消订单
    OrderCancel() {
      this.$api.maintenanceOrder.OrderCancel({
        'order_id': this.orderid
      }).then(res => {
        console.log(res)
        if (res.code === 200) {
          uni.showToast({
            title: '已取消',
            duration: 2000
          })
          setTimeout(() => {
            uni.navigateBack({
              delta: 1
            })
          }, 2000)
        } else {
          uni.showToast({
            title: '取消失败，请稍后重试！',
            icon: 'none',
            duration: 2000
          })
        }

      }).catch(err => {
        console.log(err)
      })

    },
    // 预约到店
    OrderMakeAnAppointment() {
      let data = {
        'order_id': this.orderid,
        'time':this.OrderMakeTimeStatus?'': this.OrderMakeTime
        //
      }
      if (this.OrderMakeTime) {
        this.$api.maintenanceOrder.OrderMakeAnAppointment(data).then(res => {
          console.log(res)
          if (res.code === 200) {
            uni.showToast({
              title: this.OrderMakeTimeStatus?'已取消预约到店':'已预约到店',
              duration: 2000
            })
						
            setTimeout(() => {
              uni.navigateBack({
                delta: 1
              })
            }, 2000)
          } else {
            uni.showToast({
              title: res.msg,
              duration: 2000,
              icon: 'none'
            })
          }

        }).catch(err => {
          console.log(err)
        })
      } else {
        uni.showToast({
          title: '请先选择预约时间',
          icon: 'none',
          duration: 2000
        })
      }

    },
    // 取消排队
    OrderMaintenanceCancelQueue() {

      this.$api.maintenanceOrder.OrderMaintenanceCancelQueue({
        order_id: this.orderid
      }).then(res => {
        console.log(res)
        if (res.code === 200) {
          uni.showToast({
            title: '已取消排队',
            duration: 2000
          })
          setTimeout(() => {
            uni.navigateBack({
              delta: 1
            })
          }, 2000)
        } else {
          uni.showToast({
            title:'取消失败，您有其他的订单正在施工！',
            icon: 'none',
            duration: 2000
          })
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 服务检查切换
    lineUpChange(val) {
      this.lineUpcurrent = val
    },
    goOrderInvoice() {
      uni.navigateTo({
        url: '/pages_order/orderInvoice/orderInvoice'
      })
    },
    // 分割订单号
    returntrim(id) {
      if (id) {
        let ids = id.replace(/\s/g, '').replace(/(.{4})/g, '$1 ')
        return ids
      }
    },
		getUrlParam(url) {
		  let obj = {}
		  let str = url.split('?')[1].split('&')
		  for (let i = 0; i < str.length; i++) {
		      let a = str[i].split('=')
		      obj[a[0]] = a[1]
		  }
		  return obj
		},
    // 扫码
    scan() {
      uni.scanCode({
        success: res => {

          let result = this.getUrlParam(res.result)
          console.log(result)
          if (result.state === 1) {
            uni.navigateTo({
              url: '/pages_commodity/lineUp/lineUp?storeId=' + result.id
            })
          }

        },
        fail: err => {
          console.log(err)
        }
      })
    },
    //打电话
    phoneClick(phone) {
      console.log(phone)
      // AddMobileContactData(this.$store.state.userInfoDetail.store.storeId)
      uni.makePhoneCall({
        phoneNumber: phone //仅为示例
      })
    },
    // 复制
    copy(data) {
      uni.setClipboardData({
        data: data,
        success: function() {
          console.log('success')
          uni.showToast({
            title: '复制成功'
          })
        },
      })
    },
    //在线咨询
    contact() {
      this.$store.commit('news/createConversationActive', {
        user: this.dataObj.store_name, //店铺名称
        img: this.dataObj.store_TopImg, //店铺头像
        lastTime: '',
        userId: this.dataObj.store_Id, //店铺id
        conversationID: 'C2C' + this.dataObj.store_Id,
      })
      console.log(this.dataObj)
      this.$store.commit('news/setCardInfo', {
        imgSrc: this.dataObj.maintenancetype[0].goodsDetailslist[0].goods_pic,
        name: '维保商品/服务订单',
        subName: '内含' +
					(this.dataObj.servicelist.length +
						this.dataObj.maintenancetype.length) +
					'项',
        discounts: this.dataObj.discounts,
        price: this.dataObj.amount_payable,
        type: 'afterSales',
      })
      uni.navigateTo({
        url: '/pages_common/contactSeller/contactSeller?user=' + this.dataObj.store_name + '&pageId=' +
					this.dataObj.id
      })
    },
    //我的爱车列表
    async GetCarList() {
      const value = uni.getStorageSync('Ztc_C_USER_INFO')
      if (value) {
        var ids
        ids = value.id
      }
      try {
        let data = {
          pageIndex: 1,
          pageSize: 10,
          uid: ids
        }
        const res = await this.$api.commodity.GetCarList(data)
        res.data.list.forEach((it, index) => {
          if (it.id === this.dataObj.love_car_id) {
            this.mycarindex = index
          }
        })

      } catch (e) {
        console.log(e)
      }
    },
    returnPayTyper(type) {
      let txt = ''
      if (type !== null && type !== undefined) {
        if (type === 0) {
          txt = '支付宝'
        } else if (type === 1) {
          txt = '微信'
        } else if (type === 15) {
          txt = '现金'
        }else {
          txt = '余额'
        }
      }
      return txt
    },
    goReport(state) {

      // index 当前第几个车，current 第一层滑块定位检查记录3  state 检查记录滑块定位
      uni.navigateTo({
        url: '/pages_commodity/mylovecarInformation/mylovecarInformation?index=' + this.mycarindex +
					'&current=3' +
					'&state=' + state + '&oderid=' + this.dataObj.arriveId
      })
    }

  }
}
